package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.gxuwz.medical.domain.area.Area;
import com.gxuwz.medical.vo.PageBean;

public class AreaServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String url = request.getRequestURI();
		String methodName = url.substring(url.lastIndexOf("/") + 1,
				url.lastIndexOf("."));
		Method method = null;

		try {
			method = getClass().getDeclaredMethod(methodName,
					HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void jump(HttpServletRequest request, HttpServletResponse response,String path) throws ServletException, IOException, SQLException{
		Area a = new Area();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = a.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}
	public void del(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "page/area/list.jsp";
		String areacode = request.getParameter("areacode");
		areacode = new String(areacode.getBytes("ISO-8859-1"),"UTF-8");
		Area a = new Area();
		a.delete(areacode);
		jump(request,response,path);
	}

	public void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "/page/area/list.jsp";
		Area a = new Area();
		String page = request.getParameter("page");
		if (page == null)
			page = "1";
		int currentpage = Integer.parseInt(page);
		PageBean pageBean = a.queryAll(currentpage, 5);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher(path).forward(request, response);
	}

	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "list.jsp";
		String msg = "添加失败";
		String areapcode = request.getParameter("areapcode");
		String areaname = request.getParameter("areaname");
		Area a = new Area();
		a.setAreaname(areaname);
		a.setAreapcode(areapcode);
		a.autoSet();
		a.add();
		jump(request,response,path);
		
	}

	public void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, SQLException {
		String path = "list.jsp";
		String areacode = request.getParameter("areacode");
		String areaname = request.getParameter("areaname");
		System.out.println(areaname);
		Area a = new Area();
		a.setAreaname(areaname);
		a.setAreacode(areacode);
		a.update();
		jump(request,response,path);
	}

}
